package com.hbnu.thread;

/**
 * @author kittychen
 * @since 2025-10-22 10:32
 */
public class Box {

    private int number;

    public synchronized void put() {
        while (true) {
            try {
                if (number >= 20) {
                    wait();
                } else {
                    System.out.println("生产者生产了" + number + "号牛奶");
                    number++;
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                notifyAll();
            }
        }
    }

    public synchronized void get() {
        while (true) {
            try {
                if (number <= 0) {
                    wait();
                } else {
                    number--;
                    System.out.println("消费者消费了" + number + "号牛奶");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                notifyAll();
            }
        }
    }
}
